package com.cq.hd.admin.controller;

import com.cq.hd.admin.annotation.RequiresPermissionsDesc;
import com.cq.hd.admin.biz.AdminBusinessSubsidyBiz;
import com.cq.hd.admin.po.TbBusinessAdminUserPo;
import com.cq.hd.admin.vo.AdminBusinessSubsidyVo;
import com.cq.hd.common.enums.ResponseEnums;
import com.cq.hd.common.enums.biz.BankPayTradeTypeEnum;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.ResultGenerator;
import com.cq.hd.common.response.Throw;
import com.cq.hd.common.utils.IpUtil;
import com.cq.hd.member.api.TbBankPayRecordApi;
import com.cq.hd.member.api.dto.AdminBankPayRecordPageDto;
import com.cq.hd.member.api.dto.BankPayQueryDto;
import com.cq.hd.member.api.dto.BankPayRechargeDto;
import com.cq.hd.member.api.vo.AdminBankPayRecordPageVo;
import com.cq.hd.member.api.vo.BankPayQueryVo;
import com.cq.hd.member.api.vo.BankPayRechargeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "商家后台商家补贴金相关接口")
@Slf4j
@RestController
@RequestMapping("/admin/business/subsidy")
public class AdminBusinessSubsidyController extends BaseController {

    @Autowired
    private AdminBusinessSubsidyBiz adminBusinessSubsidyBiz;

    @Autowired
    private TbBankPayRecordApi bankPayRecordApi;

    @ApiOperation(value = "补贴预充值，返回手续费，支付金额等")
    @ApiResponse(response = AdminBusinessSubsidyVo.class, code = 200, message = "接口返回对象参数")
    @RequiresPermissions("adminBusiness:adminBusinessSubsidy:preRecharge")
    @RequiresPermissionsDesc(menu = {"商家后台", "账户中心-补贴充值"}, button = "预充值")
    @GetMapping("/preRecharge")
    public Result<AdminBusinessSubsidyVo> preRecharge() {
        TbBusinessAdminUserPo businessAdminUserPo = getBusinessAdminUser();
        if (businessAdminUserPo == null) {
            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
        }

        Long businessId = businessAdminUserPo.getBusinessId();
        Long appUserId = businessAdminUserPo.getAppUserId();
        if (businessId == null || businessId <= 0 || appUserId == null || appUserId <= 0) {
            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
        }

        return ResultGenerator.success(adminBusinessSubsidyBiz.preRecharge(businessAdminUserPo));
    }

    @ApiOperation(value = "充值补贴金")
    @ApiResponse(response = BankPayRechargeVo.class, code = 200, message = "接口返回对象参数")
    @RequiresPermissions("adminBusiness:adminBusinessSubsidy:recharge")
    @RequiresPermissionsDesc(menu = {"商家后台", "账户中心-补贴充值"}, button = "充值")
    @PostMapping("/recharge")
    public Result<BankPayRechargeVo> recharge(@RequestBody BankPayRechargeDto bankPayRechargeDto, HttpServletRequest request) {
        TbBusinessAdminUserPo businessAdminUserPo = getBusinessAdminUser();
        if (businessAdminUserPo == null) {
            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
        }

        Long businessId = businessAdminUserPo.getBusinessId();
        Long appUserId = businessAdminUserPo.getAppUserId();
        if (businessId == null || businessId <= 0 || appUserId == null || appUserId <= 0) {
            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
        }

        bankPayRechargeDto.setUserId(businessAdminUserPo.getAppUserId());
        bankPayRechargeDto.setBusinessId(businessId);
        bankPayRechargeDto.setRequestIp(IpUtil.getIpAddr(request));

        return ResultGenerator.success(adminBusinessSubsidyBiz.recharge(bankPayRechargeDto));
    }

//    @ApiOperation(value = "查单")
//    @ApiResponse(response = BankPayQueryVo.class, code = 200, message = "接口返回对象参数")
//    @RequiresPermissions("adminBusiness:adminBusinessSubsidy:queryOrderPay")
//    @RequiresPermissionsDesc(menu = {"商家后台", "账户中心-补贴充值"}, button = "查单")
//    @PostMapping("/queryOrderPay")
//    public Result<BankPayQueryVo> queryOrderPay(@RequestBody BankPayQueryDto bankPayQueryDto) {
//        TbBusinessAdminUserPo businessAdminUserPo = getBusinessAdminUser();
//        if (businessAdminUserPo == null) {
//            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
//        }
//
//        Long businessId = businessAdminUserPo.getBusinessId();
//        Long appUserId = businessAdminUserPo.getAppUserId();
//        if (businessId == null || businessId <= 0 || appUserId == null || appUserId <= 0) {
//            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
//        }
//
//        bankPayQueryDto.setUserId(businessAdminUserPo.getAppUserId());
//        bankPayQueryDto.setBusinessId(businessId);
//
//        return bankPayRecordApi.queryOrderPay(bankPayQueryDto);
//    }

    @ApiOperation(value = "网银支付的充值补贴金记录")
    @ApiResponse(response = AdminBankPayRecordPageVo.class, code = 200, message = "接口返回对象参数")
    @RequiresPermissions("adminBusiness:adminBusinessSubsidy:list")
    @RequiresPermissionsDesc(menu = {"商家后台", "账户中心-补贴充值"}, button = "查询")
    @GetMapping("/page")
    public Result<Page<AdminBankPayRecordPageVo>> pageAdminBankPayRecord(AdminBankPayRecordPageDto adminBankPayRecordPageDto) {
        Long businessId = getBusinessId();
        if (businessId == null || businessId <= 0) {
            Throw.isBusinessException(ResponseEnums.USER_ACCESS_DENIED);
        }

        adminBankPayRecordPageDto.setTradeType(BankPayTradeTypeEnum.RECHARGE_SUBSIDY.getValue());
        adminBankPayRecordPageDto.setBusinessId(businessId);
        return bankPayRecordApi.pageAdminBankPayRecord(adminBankPayRecordPageDto);
    }


}
